#!/usr/bin/env python

"""
    Los.py
    This file contains LOS procedures
"""

# Imports
from GameMap import *

class Bresenham(object):
    def __init__(self, gameUI, gameMap, gameObjects):
        self.gameUI = gameUI
        self.gameMap = gameMap
        self.gameObjects = gameObjects

    def check(self, x1, x2, y1, y2):
        self.deltax = abs(x2 - x1)
        self.deltay = abs(y2 - y1)

        if self.deltax >= self.deltay:
            # x is independant
            self.steps = self.deltax + 1
            self.d = (2 * self.deltay) - self.deltax

            self.dinc1 = self.deltay << 1
            self.dinc2 = (self.deltay - self.deltax) << 1

            self.xinc1 = 1
            self.xinc2 = 1
            self.yinc1 = 0
            self.yinc2 = 1
        else:
            # y is independant
            self.steps = self.deltay + 1
            self.d = (2 * self.deltax) - self.deltay

            self.dinc1 = self.deltax << 1
            self.dinc2 = (self.deltax - self.deltay) << 1

            self.xinc1 = 0
            self.xinc2 = 1
            self.yinc1 = 1
            self.yinc2 = 1

        if x1 > x2:
            self.xinc1 = -self.xinc1
            self.xinc2 = -self.xinc2

        if y1 > y2:
            self.yinc1 = -self.yinc1
            self.yinc2 = -self.yinc2

        self.x = x1
        self.y = y1

        self.blocked = False
        for i in range(self.steps):
            if self.gameMap.map[self.y][self.x].block_los == True:
                self.blocked = True

            if self.d < 0:
                self.d += self.dinc1
                self.x += self.xinc1
                self.y += self.yinc1
            else:
                self.d += self.dinc2
                self.x += self.xinc2
                self.y += self.yinc2